package com.market.domain.transients;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import com.market.domain.BaseObject;

public class MarketDate extends BaseObject implements Comparable<MarketDate> {

	/**
	 * 
	 */
	private static final long serialVersionUID = -2949689788264146492L;

	private Calendar calendar;

	private String timeRangeLabel; // eg: 16:00-22:00

	public MarketDate(Calendar calendar) {
		this.calendar = calendar;
	}

	public MarketDate(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		this.calendar = cal;
	}
	
	
	@Override
	public void setId(Long id) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public Long getId() {
		// TODO Auto-generated method stub
		return null;
	}

	public int compareTo(MarketDate o) {
		return this.calendar.compareTo(o.getCalendar());
	}

	/**
	 * Check specific required date against current date and time, to see the order can be confirm.
	 * (Use order or buy).
	 * @return
	 */
	public Boolean getCanConfirm() {
		Calendar now = Calendar.getInstance(Locale.CANADA);
		if (now.get(Calendar.YEAR) == this.calendar.get(Calendar.YEAR)
				&& now.get(Calendar.DAY_OF_YEAR) == this.calendar
						.get(Calendar.DAY_OF_YEAR)
				&& now.get(Calendar.HOUR_OF_DAY) < 10) { // Stop accept buy
															// after
															// 10:00 Am of
															// required
															// day
			return Boolean.TRUE;
		} else {
			if (now.get(Calendar.YEAR) == this.calendar.get(Calendar.YEAR)
					&& (this.calendar.get(Calendar.DAY_OF_YEAR)
							- now.get(Calendar.DAY_OF_YEAR) == 1)
					&& now.get(Calendar.HOUR_OF_DAY) >= 10) { // Stop accept
																// order, Start
																// accept buy
																// after 10:00
																// am of
																// the day
																// before
																// required day
				return Boolean.TRUE;
			} else {
				if ((this.calendar.get(Calendar.YEAR) - now.get(Calendar.YEAR)) == 1
						&& now.get(Calendar.DAY_OF_YEAR) == now
								.getMaximum(Calendar.DAY_OF_YEAR)
						&& this.calendar.get(Calendar.DAY_OF_YEAR) == this.calendar
								.getMinimum(Calendar.DAY_OF_YEAR)
						&& now.get(Calendar.HOUR_OF_DAY) >= 10) {// This is an
																	// exception
																	// of
																	// Stop
																	// accept
																	// order,
																	// start
																	// accept
																	// buy,
																	// The
																	// required
																	// date is
																	// the
																	// first day
																	// of
																	// next
																	// year. And
																	// current
																	// date is
																	// last day
																	// of
																	// current
																	// year.
					return Boolean.TRUE;
				}
			}
		}

		return Boolean.FALSE; // Other time accept order, not buy
	}

	
	/**
	 * Use this method to check if the specific required date is pasted, means customer can not order and buy on 
	 * the required date any more.
	 * @return
	 */
	public Boolean getIsPasted() {
		Calendar now = Calendar.getInstance(Locale.CANADA);

		if ((now.get(Calendar.YEAR) > this.calendar.get(Calendar.YEAR))
				|| (now.get(Calendar.YEAR) == this.calendar.get(Calendar.YEAR) && (now
						.get(Calendar.DAY_OF_YEAR) > this.calendar
						.get(Calendar.DAY_OF_YEAR)))
				|| ((now.get(Calendar.YEAR) == this.calendar.get(Calendar.YEAR) && (now
						.get(Calendar.DAY_OF_YEAR) == this.calendar
						.get(Calendar.DAY_OF_YEAR))) && now
						.get(Calendar.HOUR_OF_DAY) >= 9)) {

			return Boolean.TRUE;
		}
		return Boolean.FALSE;
	}
	
	
	/**
	 * Use this method to check if the specific required date is true pasted, means even administrator can not order and buy for
	 * customer on  the required date any more.
	 * @return
	 */
	public Boolean getIsTruePasted() {
		Calendar now = Calendar.getInstance(Locale.CANADA);

		if ((now.get(Calendar.YEAR) > this.calendar.get(Calendar.YEAR))
				|| (now.get(Calendar.YEAR) == this.calendar.get(Calendar.YEAR) && (now
						.get(Calendar.DAY_OF_YEAR) > this.calendar
						.get(Calendar.DAY_OF_YEAR)))
				|| ((now.get(Calendar.YEAR) == this.calendar.get(Calendar.YEAR) && (now
						.get(Calendar.DAY_OF_YEAR) == this.calendar
						.get(Calendar.DAY_OF_YEAR))) && now
						.get(Calendar.HOUR_OF_DAY) >= 16)) {

			return Boolean.TRUE;
		}
		return Boolean.FALSE;
	}

	/**
	 * Use this method to check if customer can submit the order on specific time.
	 * It is 10 ten minutes plus is pasted time.
	 * @return
	 */
	public Boolean getIsCanSubmit() {
		Calendar now = Calendar.getInstance(Locale.CANADA);

		if ((now.get(Calendar.YEAR) > this.calendar.get(Calendar.YEAR))
				|| (now.get(Calendar.YEAR) == this.calendar.get(Calendar.YEAR) && (now
						.get(Calendar.DAY_OF_YEAR) > this.calendar
						.get(Calendar.DAY_OF_YEAR)))
				|| ((now.get(Calendar.YEAR) == this.calendar.get(Calendar.YEAR) && (now
						.get(Calendar.DAY_OF_YEAR) == this.calendar
						.get(Calendar.DAY_OF_YEAR))) && now
						.get(Calendar.HOUR_OF_DAY) >= 10 && now.get(Calendar.MINUTE) > 10) ) {

			return Boolean.FALSE;
		}
		return Boolean.TRUE;
	}
	
	
	public String getTimeRangeLabel() {
		return timeRangeLabel;
	}

	public void setTimeRangeLabel(String timeRangeLabel) {
		this.timeRangeLabel = timeRangeLabel;
	}

	public String getDayOfWeekLabel() {
		return super.getMessage("DAY_OF_WEEK_"
				+ calendar.get(Calendar.DAY_OF_WEEK));
	}

	public int getDayOfWeek() {
		return calendar.get(Calendar.DAY_OF_WEEK);
	}

	public String getDateLabel() {
		SimpleDateFormat formate = new SimpleDateFormat("yyyy-MM-dd");
		return formate.format(this.calendar.getTime());
	}

	public Date getDate() {
		return this.calendar.getTime();
	}

	public Calendar getCalendar() {
		return calendar;
	}

	public void setCalendar(Calendar calendar) {
		this.calendar = calendar;
	}

	/**
	 * Return a new instance of MarketDate representing the next day of current
	 * MarketDate;
	 * 
	 * @return
	 */

	public MarketDate nextDay() {
		Calendar newCalendar = (Calendar) this.calendar.clone();
		if (newCalendar.get(Calendar.DAY_OF_YEAR) == newCalendar
				.getMaximum(Calendar.DAY_OF_YEAR)) {
			newCalendar.roll(Calendar.YEAR, true);
			newCalendar.set(Calendar.DAY_OF_YEAR, 1);
		} else {
			newCalendar.roll(Calendar.DAY_OF_YEAR, true);
		}
		return new MarketDate(newCalendar);
	}

	public Integer getWeekOfYear() {
		return this.calendar.get(Calendar.WEEK_OF_YEAR);
	}

	public String getWeekOfYearLabel() {
		return "Week " + this.calendar.get(Calendar.WEEK_OF_YEAR);
	}

	public String getYearLabel() {
		return "Year " + this.calendar.get(Calendar.YEAR);
	}

	public Integer getYear() {
		return 2013;
//		return this.calendar.get(Calendar.YEAR);
	}

	public Integer getDayOfYear() {
		return this.calendar.get(Calendar.DAY_OF_YEAR);
	}

	public String getKey() {
		return String.valueOf(this.calendar.get(Calendar.DAY_OF_YEAR))
				+ String.valueOf(this.calendar.get(Calendar.YEAR));
	}

	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean equals(Object o) {
		if (o instanceof MarketDate) {
			MarketDate marketDate = (MarketDate) o;
			if (this.getYear().equals(marketDate.getYear())
					&& this.getDayOfYear().equals(marketDate.getDayOfYear())) {
				return true;
			}
		}
		return false;
	}

	@Override
	public boolean equalsId(Object o) {
		if (o instanceof MarketDate) {
			return this.calendar.equals(((MarketDate) o).getCalendar());
		}
		return false;
	}

	@Override
	public int hashCode() {
		// TODO Auto-generated method stub
		return this.calendar.hashCode();
	}

	

}
